"""author: wangxin
   data: 2021/11/12
   project: 王鑫 函数作业.py
"""

# 互送礼活动
import random

lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
receive = []  # 存放已经收过礼物的员工
for i in lst:
    lst = list(set(lst) - {i})  # 把自己从名单中删掉 自己不能送给自己
    if len(lst) == 2 and 'h' in lst and 'h' not in receive:  # 若倒数第二个人送礼时，最后一个人仍未收到礼物，只能将礼物送给最后一个人
        man = 'h'
        print(f"{i}的礼物送给了{man}")
    else:
        man = random.choice(lst)  # 随机选出一个收礼物的人
        receive += [man]
        print(f"{i}的礼物送给了{man}")

    if i not in receive:
        lst = list(set(lst) - {man}) + [i]  # 如果i还没有收到过礼物就把i放回名单中，并把这次收到礼物的人从名单中删除
    else:
        lst = list(set(lst) - {man})  # 把这次收到礼物的人从名单中删除


# 用户密码复杂度验证
pwd = str(input("请输入你的密码："))


def lenth(pwd):
    if len(pwd) > 8:
        return 1
    else:
        return 0


def upper(pwd):
    for i in pwd:
        a = ord(i)
        if 65 <= a <= 90:
            return 1
    return 0


def lower(pwd):
    for i in pwd:
        a = ord(i)
        if 97 <= a <= 122:
            return 1
    return 0


def number(pwd):
    for i in pwd:
        a = ord(i)
        if 48 <= a <= 57:
            return 1
    return 0


def symbol(pwd):
    for i in pwd:
        a = ord(i)
        if a < 48 or 57 < a < 65 or 90 < a < 97 or a > 122:
            return 1
    return 0


def repeat(pwd):
    for i in range(len(pwd)):
        if pwd.count(pwd[i:i + 3]) < 2:
            return 1
        return 0


def main(pwd):
    pwd_level = 2
    lst1 = []
    if lenth(pwd):
        pwd_level += 1
    else:
        lst1 += ['密码长度不足8位']

    if upper(pwd) + lower(pwd) + number(pwd) + symbol(pwd) >= 3:
        pwd_level += 1
    else:
        lst1 += ['密码没有由三种或三种以上字符组成']

    if repeat(pwd):
        pwd_level += 1
    else:
        lst1 += ['密码有长度超过或等于3的子串重复']

    if lst1 == []:
        lst1 = ["恭喜你的密码强度合格"]

    print(f"你的密码强度为{pwd_level}颗星", lst1)


main(pwd)



#三乐购物系统

user_message = []
goods = {'1': {'name': '苹果', 'price': 1.2}, '2': {'name': '香蕉', 'price': 1.4}}
shopcar = []


def interface():
    print("欢迎使用三乐购物系统".center(50, '#'))
    print("1.用户注册")
    print("2.用户登录")
    print("3.退出")
    a = int(input("请输入1/2/3:"))
    while 1:
        if a == 1:
            register()
            break
        elif a == 2:
            login()
            break
        elif a == 3:
            print("已退出")
            break
        else:
            print("输入有误，请重新输入")
            continue


def register():
    print('注册界面'.center(50, '#'))
    user_name = input("请输入你的用户名：")
    user_passwd = input("请输入你的密码：")
    recharge_passwd = input("请输入你的充值密码")
    message = {user_name: {'密码': user_passwd, '充值密码': recharge_passwd, '余额': 0}}
    user_message.append(message)
    print("注册成功，快去登录吧")
    interface()


def login():
    print("登录界面".center(50, '#'))
    while 1:
        global name
        name = input("请输入用户名:")
        for i in user_message:
            if name not in i.keys():
                print("用户名不存在，请重新输入")
                login()
                break
            pwd = input("请输入密码:")
            if pwd == i[name]['密码']:
                print(f"{name}登录成功！")
                shop()
                break
            else:
                print("密码不正确，请重新输入")
                login()
                break


def shop():
    print("三乐购物系统".center(50, '#'))
    print("1、查看商品")
    print("2、账户充值")
    print("3、购买商品")
    print("4、查看购物车")
    print("5、退出系统")
    while 1:
        option = int(input("请选择以上功能并输入其编号："))
        if option == 1:
            cheak_goods()
            break
        if option == 2:
            recharge()
            break
        if option == 3:
            buy()
            break
        if option == 4:
            cheak_cart()
            break
        if option == 5:
            print("欢迎下次使用，已退出")
            break
        else:
            print("输入有误，请重新输入")
            continue


def cheak_goods():
    print("今日商品".center(50, '#'))
    print(goods)
    shop()


def recharge():
    print("充值界面".center(50, "#"))
    while 1:
        r_pwd = input("请输入充值密码：")
        for i in user_message:
            if r_pwd == i[name]['充值密码']:
                r_money = int(input("请输入你要输入的金额："))
                i[name]['余额'] += r_money
                print(f"充值成功！你的当前余额为{i[name]['余额']}")
                shop()
                break


def buy():
    print("购买界面".center(50, '#'))
    print(goods)
    while 1:
        code = input("请输入你要购买的商品代号：")
        if code in goods.keys():
            count = int(input(f"请输入你要购买{goods[code]['name']}的数量:"))
            for i in user_message:
                if i[name]['余额'] >= count * goods[code]['price']:
                    i[name]['余额'] -= count * goods[code]['price']
                    shopcar.append(goods[code]['name'])
                    print(f"已购买{count}个{goods[code]['name']},你的余额为{i[name]['余额']}")
                    shop()
                else:
                    print("您的余额不足，请充值")
                    recharge()
                    break

        else:
            print("没有这个商品，请重新输入")
            buy()
            break


def cheak_cart():
    if shopcar == []:
        print("这里空空如也，快去购物吧")
        shop()
    else:
        print(f"你的购物车:{shopcar}")
        shop()


interface()